package portlet.common;

import java.util.List;

import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.Row;
import oracle.jbo.RowIterator;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewObject;
import oracle.jbo.domain.Number;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

import portlet.common.search.AdvancedSearchGroup;

public class Utils {
    public Utils() {
        super();
    }
    
    public static DCIteratorBinding getIteratorBinding(String iteratorName) {
        return ((DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry()).findIteratorBinding(iteratorName);
    }

    public static void searchadvanced(String iteratorName,
                                      List<AdvancedSearchGroup> listAdvancedSearchGroup) {
        DCIteratorBinding iterator=
            Utils.getIteratorBinding(iteratorName);
        ViewObject vo = iterator.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        AdvancedSearchGroup.setViewCriteria(listAdvancedSearchGroup, vc);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.getViewCriteriaManager().applyViewCriteria(vc);
        vo.executeQuery();
        iterator.executeQuery();
    }
    
    public static int getPageSize(String iteratorName){
        int pageSize = 20;
        String pageSizeString=Utils.ENUM_CAU_HINH_KHOA.PAGE_SIZE.getValue(iteratorName);
        try{
            if(pageSizeString!=null){
                pageSize=new Integer(pageSizeString);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return pageSize;
    }
    
    public static void runJavascript(String funcName){
        FacesContext context = FacesContext.getCurrentInstance();
        ExtendedRenderKitService extRenderKitSrvc =
            Service.getRenderKitService(context,
                                        ExtendedRenderKitService.class);
        extRenderKitSrvc.addScript(context, funcName);        
    }

    public static enum ENUM_KIEU_DU_LIEU {
        NUMBER(1, "NUMBER"),
        VARCHAR(2, "VARCHAR"),
        DATE(3, "DATE"),
        LINK(7, "LINK"),
        EMAIL(8, "EMAIL"),
        CLOB(4, "CLOB"),
        BLOB(5, "BLOB"),
        FILE(6, "FILE"),
        LOGO(9, "LOGO"),
        SAN_GIAO_DICH(101, "SAN_GIAO_DICH"),
        LOAI_CHUNG_KHOAN(102, "LOAI_CHUNG_KHOAN"),
        KIEM_TOAN_VIEN(103, "KIEM_TOAN_VIEN"),
        SL_KIEM_TOAN_VIEN(104, "SL_KIEM_TOAN_VIEN"),
        IN_OUT_KIEM_TOAN_VIEN(105, "IN_OUT_KIEM_TOAN_VIEN");
        int id;
        String ma;

        private ENUM_KIEU_DU_LIEU(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_BAT_BUOC {
        KHONG_BAT_BUOC(1, "KHONG_BAT_BUOC"),
        BAT_BUOC(2, "BAT_BUOC");
        int id;
        String ma;

        private ENUM_BAT_BUOC(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_TRANG_THAI {
        KHONG_CONG_BO(1, "KHONG_CONG_BO"),
        CONG_BO(2, "CONG_BO");
        int id;
        String ma;

        private ENUM_TRANG_THAI(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_BAO_CAO {
        KHONG_PHAI_BAO_CAO(1, "KHONG_PHAI_BAO_CAO"),
        BAO_CAO(2, "BAO_CAO");
        int id;
        String ma;

        private ENUM_BAO_CAO(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_LOAI_TRUONG_TT {
        DON_VI(1, "DON_VI"),
        BAO_CAO(2, "BAO_CAO"),
        CONG_BO_THONG_TIN(3, "CONG_BO_THONG_TIN"),
        SAN_GIAO_DICH(4, "SAN_GIAO_DICH"),
        LOAI_CHUNG_KHOAN(5, "LOAI_CHUNG_KHOAN"),
        KIEM_TOAN_VIEN(6, "KIEM_TOAN_VIEN");
        int id;
        String ma;

        private ENUM_LOAI_TRUONG_TT(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_VI_TRI_HIEN_THI {
        TABLE(1, "TABLE"),
        SEARCH(2, "SEARCH"),
        SELECT(4, "SELECT"),
        PORTLET_TABLE(8, "PORTLET_TABLE"),
        PORTLET_SEARCH(16, "PORTLET_SEARCH"),
        PORTLET_INFO(32, "PORTLET_INFO"),
        PORTLET_SELECT(64, "PORTLET_SELECT"),
        PORTLET_TIN_TIEU_DE(128, "PORTLET_TIN_TIEU_DE"),
        PORTLET_TIN_TOM_TAT(512, "PORTLET_TIN_TOM_TAT"),
        PORTLET_TIN_INFO(1024, "PORTLET_TIN_INFO");
        int id;
        String ma;

        private ENUM_VI_TRI_HIEN_THI(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public void setMa(String ma) {
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_CBTT_VI_TRI_HIEN_THI {
        CBTT(1, "CBTT"),
        BC(2, "BC");
        int id;
        String ma;

        private ENUM_CBTT_VI_TRI_HIEN_THI(int id, String ma) {
            this.id = id;
            this.ma = ma;
        }

        public void setMa(String ma) {
            this.ma = ma;
        }

        public String getMa() {
            return ma;
        }

        public int getId() {
            return id;
        }
    }

    public static enum ENUM_CAU_HINH_KHOA {
        FILE_ROOT("FILE_ROOT"),
        DLCT_DIR("DLCT_DIR"),
        DTVB_DIR("DTVB_DIR"),
        PAKN_DIR("PAKN_DIR"),
        APTC_DIR("APTC_DIR"),
        TTHC_DIR("TTHC_DIR"),
        KNTC_DIR("KNTC_DIR"),
        PAGE_SIZE("PAGE_SIZE");
        String khoa;

        private ENUM_CAU_HINH_KHOA(String khoa) {
            this.khoa = khoa;
        }

        public String getValue(String iteratorName) {
            DCIteratorBinding it = Utils.getIteratorBinding(iteratorName);
            it.setRangeSize(-1);
            it.executeQuery();
            RowIterator rowIterator =
                it.findRowsByAttributeValues(new String[] { "Khoa" },
                                             new Object[] { khoa });
            if (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                return (String)row.getAttribute("GiaTri");
            }
            return null;
        }

        private String khoaHt = "/c:/root";

        public String KHOA_HT() {
            return khoaHt;
        }

        private String khoaAptcImage = "aptc/images/";

        public String KHOA_APTC_IMAGE() {
            return khoaAptcImage;
        }

        private String khoaAptcFile = "aptc/file/";

        public String KHOA_APTC_FILE() {
            return khoaAptcFile;
        }

        private String khoaTthcFile = "tthc/file/";

        public String KHOA_TTHC_FILE() {
            return khoaTthcFile;
        }

        public String getKhoa() {
            return khoa;
        }
    }

    public static enum ENUM_MA_UNG_DUNG {
        APTC(1),
        PAKN(2),
        TTPH(3),
        DLCT(4),
        TTHC(5),
        DTVB(6),
        KNTC(7);
        Number maUngDung;

        private ENUM_MA_UNG_DUNG(int maUngDung) {
            this.maUngDung = new Number(maUngDung);
        }

        public void setMaUngDung(Number maUngDung) {
            this.maUngDung = maUngDung;
        }

        public Number getMaUngDung() {
            return maUngDung;
        }
    }
}
